
package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.BoxOutStockAdapter;
import com.mflex.WHSmartWhouseAd.model.MaterialTaskModel;
import com.mflex.WHSmartWhouseAd.model.TaskForRawBoxModel;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;

import java.util.List;
import java.util.Objects;

public class BoxOutStockActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText stvTaskNo;
    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;

    String userName;
    List<TaskForRawBoxModel> TaskForRawBoxModelList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_box_outstock);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stvTaskNo= findViewById(R.id.stvTaskNo);
        txtList= findViewById(R.id.lvList);


        Intent intent = getIntent();

        userName = intent.getStringExtra("userName");

        stvTaskNo.requestFocus();

        stvTaskNo.setOnKeyListener((view, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                if(StringUtils.equals("",stvTaskNo.getText().toString())) {
                    mMiniLoadingDialog.dismiss();
                    MediaPlayer mp = MediaPlayer.create(BoxOutStockActivity.this, R.raw.error_voice);mp.start();
                    XToastUtils.success("请先扫任务单", 1500);
                    stvTaskNo.setText("");
                    stvTaskNo.requestFocus();
                    return false;
                }
                mMiniLoadingDialog.show();
                XHttp.get("api/WHSmartWhousePDA/GetSmartTaskListForRawBox")
                        .params("TaskNo", stvTaskNo.getText().toString())
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(120000)
                        .execute(new SimpleCallBack<List<TaskForRawBoxModel>>() {
                            @Override
                            public void onSuccess(List<TaskForRawBoxModel> response) {
                                mMiniLoadingDialog.dismiss();
                                if(response==null)
                                {
                                    mMiniLoadingDialog.dismiss();
                                    MediaPlayer mp = MediaPlayer.create(BoxOutStockActivity.this, R.raw.error_voice);mp.start();
                                    XToastUtils.success("GetSmartTaskListForRaw:返回的Data=null", 1500);
                                    stvTaskNo.setText("");
                                    stvTaskNo.requestFocus();
                                    return ;
                                }
                                TaskForRawBoxModelList=response;
                                BoxOutStockAdapter MaterialOutStockDetailAdapter = new BoxOutStockAdapter(BoxOutStockActivity.this, TaskForRawBoxModelList);
                                txtList.setAdapter(MaterialOutStockDetailAdapter);
                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                stvTaskNo.setText("");
                                stvTaskNo.requestFocus();
                                MediaPlayer mp = MediaPlayer.create(BoxOutStockActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(view.getContext())
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(Objects.requireNonNull(e.getMessage()))
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });
            }
            return false;
        });

        txtList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(BoxOutStockActivity.this, BoxOutStockTaskDetailActivity.class);
                intent.putExtra("TaskNo", TaskForRawBoxModelList.get(i).getTaskNo());
                intent.putExtra("userName", userName);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}



